Skip to content

How to install wordpress with docker on ubuntu

Published: at 03:22 PM

Set up multiple WordPress sites on Ubuntu using Docker Compose and Nginx.

Table of contents

Open Table of contents

Prerequisites and usage

Step 1: Install docker and nginx

  1. Update the system:

    sudo apt update
    sudo apt upgrade -y
  2. Install the necessary dependencies:

    sudo apt install build-essential
  3. Install Nginx:

     sudo apt install nginx
  4. Install Docker

    sudo apt install apt-transport-https ca-certificates curl software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
    apt-cache policy docker-ce
    sudo apt install docker-ce
  5. Install Docker Compose

    sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose

Step 2: Create a Docker Compose File and Run

  1. Create a directory for your WordPress project and navigate into it::

    mkdir your-domain
    cd your-domain
  2. Create a stack.yml file with the following content:

    nano stack.yml

    Paste following code to file:

    version: '3.3'
    services:
    wordpress_your_domain:
        image: wordpress
        restart: always
        ports:
        - 8000:80
        environment:
        WORDPRESS_DB_HOST: db_your_domain
        WORDPRESS_DB_USER: your_domain
        WORDPRESS_DB_PASSWORD: your_password
        WORDPRESS_DB_NAME: your_domain
        volumes:
        - ./data:/var/www/html
    
    db_your_domain:
        image: mysql:5.7
        restart: always
        environment:
        MYSQL_DATABASE: your_domain
        MYSQL_USER: your_domain
        MYSQL_PASSWORD: your_password
        MYSQL_RANDOM_ROOT_PASSWORD: '1'
        volumes:
        - db_your_domain:/var/lib/mysql
    volumes:
        db_your_domain:
  3. Run docker with docker compose compose:

    docker-compose -f stack.yml up -d

Step 3: Point domain to a WordPress with Nginx

  1. Set Up DNS Records: First, make sure your domain’s DNS settings are configured to point to your server’s IP address. This typically involves setting an A record for your domain (e.g., example.com) and any subdomains (e.g., www.example.com) to your server’s IP address.
  2. Configure Nginx for Your Domain Create an Nginx configuration file for your domain. Replace example.com with your actual domain name.
    sudo nano /etc/nginx/conf.d/example.com.conf
    Add the following content to the file:
       server {
          listen 80;
          server_name example.com www.example.com;
          client_max_body_size 64M;
          location / {
             # point domain to docker port we setting in file stack.yml
             proxy_pass http://localhost:8000;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header X-Forwarded-Proto $scheme;
             proxy_redirect off;
             proxy_buffering off;
          }
       }
  3. Restart Nginx:
    sudo systemctl restart nginx

Step 4: Install wordpress on your domain

You will see tutorial setup wordpress when access to your domain. With this, you have completed the setup of wordpress on ubuntu with docker and nginx.